﻿using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Furion.CMS.Core
{
    /// <summary>
    /// 分布式缓存扩展
    /// </summary>
    /// <typeparam name="TCacheItem"></typeparam>
    public interface IDistributedCache<TCacheItem>
    {
        /// <summary>
        /// 获取缓存键值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        /// <returns></returns>
        TCacheItem Get(string key, bool hideErrors = true);
        /// <summary>
        /// 获取缓存键值(异步)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task<TCacheItem> GetAsync(string key, bool hideErrors = true, CancellationToken token = default);
        /// <summary>
        /// 批量获取缓存键值(异步)
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task<KeyValuePair<string, TCacheItem>[]> GetManyAsync(IEnumerable<string> keys, bool hideErrors = true, CancellationToken token = default);
        /// <summary>
        /// 获取或添加
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <param name="hideErrors"></param>
        /// <param name="factoryOptions"></param>
        /// <returns></returns>
        TCacheItem GetOrAdd(string key, Func<TCacheItem> factory, DistributedCacheEntryOptions factoryOptions = default, bool hideErrors = true);
        /// <summary>
        /// 获取或添加(异步)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="factory"></param>
        /// <param name="factoryOptions"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task<TCacheItem> GetOrAddAsync(string key, Func<Task<TCacheItem>> factory, DistributedCacheEntryOptions factoryOptions = default, bool hideErrors = true, CancellationToken token = default);
        /// <summary>
        /// 批量获取或添加(异步)
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="factory"></param>
        /// <param name="factoryOptions"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task<KeyValuePair<string, TCacheItem>[]> GetOrAddManyAsync(IEnumerable<string> keys, Func<IEnumerable<string>, Task<List<KeyValuePair<string, TCacheItem>>>> factory, DistributedCacheEntryOptions factoryOptions = default, bool hideErrors = true, CancellationToken token = default);

        /// <summary>
        /// 设置缓存键值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="options"></param>
        /// <param name="hideErrors"></param>
        void Set(string key, TCacheItem value, DistributedCacheEntryOptions options = default, bool hideErrors = true);
        /// <summary>
        /// 设置缓存键值(异步)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="options"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = default, bool hideErrors = true, CancellationToken token = default);
        /// <summary>
        /// 批量设置缓存键值(异步)
        /// </summary>
        /// <param name="items"></param>
        /// <param name="options"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task SetManyAsync(IEnumerable<KeyValuePair<string, TCacheItem>> items, DistributedCacheEntryOptions options = default, bool hideErrors = true, CancellationToken token = default);

        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        void Refresh(string key, bool hideErrors = true);
        /// <summary>
        /// 刷新缓存(异步)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task RefreshAsync(string key, bool hideErrors = true, CancellationToken token = default);

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        void Remove(string key, bool hideErrors = true);
        /// <summary>
        /// 删除缓存(异步)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hideErrors"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        Task RemoveAsync(string key, bool hideErrors = true, CancellationToken token = default);
    }
}
